package org.jeecg.modules.com.pointoperation.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.com.pointoperation.entity.Fillintegralrecord;
import org.jeecg.modules.com.pointoperation.entity.Pointflow;
import org.jeecg.modules.com.pointoperation.entity.Userspoint;
import org.jeecg.modules.com.pointoperation.entity.VerifyConsumeRecord;
import org.jeecg.modules.com.pointoperation.mapper.FillintegralrecordMapper;
import org.jeecg.modules.com.pointoperation.service.IFillintegralrecordService;
import org.jeecg.modules.com.pointoperation.service.IPointflowService;
import org.jeecg.modules.com.pointoperation.service.IUserspointService;
import org.jeecg.modules.com.pointoperation.service.IVerifyConsumeRecordService;
import org.jeecg.modules.com.pointoperation.utils.SignatureUtil;
import org.jeecg.modules.com.pointoperation.utils.StringClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @Description: 补积分记录
 * @Author: jeecg-boot
 * @Date:   2020-05-05
 * @Version: V1.0
 */
@Slf4j
@Service
public class FillintegralrecordServiceImpl extends ServiceImpl<FillintegralrecordMapper, Fillintegralrecord> implements IFillintegralrecordService {
    @Autowired
    private IUserspointService userspointService;
    @Autowired
    private IPointflowService pointflowService;
    @Autowired
    private IVerifyConsumeRecordService verifyConsumeRecordService;

    @Value("${APP_URL_POINT}")
    String APP_URL;

    @Override
    @Transactional
    public Fillintegralrecord fillIntegralRecord(Fillintegralrecord fillintegralrecord) {
//        fillintegralrecord.setChannel("BJQ2");

        StringClass stringClass = new StringClass();

        String boNo = stringClass.GetRandomNumber(8);
        //随机生成6位的订单号
        int boNoInt = Integer.valueOf(boNo);

        fillintegralrecord.setNote("boNo="+boNo+";"+ fillintegralrecord.getNote());
        fillintegralrecord.setBono(boNo);

        Pointflow pointflow = new Pointflow();
        pointflow.setChannel(fillintegralrecord.getChannel());
        pointflow.setOpenid(fillintegralrecord.getAppletopenid());
        pointflow.setCount(fillintegralrecord.getIntegralnumber());
        pointflow.setParam02(boNo);
        pointflow.setOperation("add");
        pointflow.setOperationtime(new Date());

        Userspoint userspoint = new Userspoint();
        userspoint.setOpenid(fillintegralrecord.getAppletopenid());
        userspoint.setChannel(fillintegralrecord.getChannel());

        VerifyConsumeRecord verifyConsumeRecord = new VerifyConsumeRecord();

        verifyConsumeRecord.setBono(boNo);
        verifyConsumeRecord.setQrCode(fillintegralrecord.getInnercode());
        verifyConsumeRecord.setVerifyConsumeTime(new Date());
        verifyConsumeRecord.setUserNo(fillintegralrecord.getAppletopenid());
        verifyConsumeRecord.setLotteryInfoId(0);
        verifyConsumeRecord.setActivityId(500);

        Boolean fillResult = this.save(fillintegralrecord);
        Boolean addVerifyRecord = this.verifyConsumeRecordService.insertVerifyRecord(verifyConsumeRecord);
//        this.pointflowService.insertPointFlow(pointflow);
//
//        this.userspointService.updateUserPointNumber(userspoint);

        JSONObject result = addPoint(fillintegralrecord.getAppletopenid(), boNoInt, 1);
        log.info( "补积分结果 ："+ result.toJSONString());

        return fillintegralrecord;
    }


    public final static String PPPCHANNEL = "BJQ2";


    public JSONObject addPoint(String member, int boNo, int totalPoint){
        JSONObject ret = new JSONObject();
        try {
            //时间戳
            String ts = String.valueOf(System.currentTimeMillis() / 1000);
            //生成签名
            JSONObject json = new JSONObject();
            json.put("userNo",member);
            json.put("boNo",boNo);
            json.put("totalPoint",totalPoint);
            json.put("ts",ts);
            String md5Key = "xuanzhen";
            String sign = SignatureUtil.makeSignature(json,md5Key);

            ret = addPointHandle(member, boNo,totalPoint,ts,sign);

/*            ret.setStatus(Integer.parseInt(jsonObject.getString("status")));
            ret.setMessage(jsonObject.getString("message"));
            ret.setPoint(Integer.parseInt(jsonObject.getString("point")));
            ret.setSn(jsonObject.getString("sn"));
            Map<String,Object> map = new HashMap<>();
            map.put("BagNo",jsonObject.getString("BagNo"));
            ret.setMemberExt(map);*/
        } catch (Exception e) {
            log.error(e.toString()+ "加分接口异常");
//            ret.setStatus(2);
//            ret.setMessage("加分接口异常");
        }
        return ret;
    }


    /**
     * 增加积分
     * @param openId
     * @param boNo
     * @param point
     * @param ts
     * @param sign
     * @return
     */
    public  JSONObject addPointHandle(String openId, Integer boNo, Integer point, String ts, String sign) {

        /**
         * 获取积分平台的channel
         */
        String pppchannel = "BJQ2";
        if (StringUtils.isBlank(pppchannel)) {
            pppchannel = "default";
        }
        pppchannel = PPPCHANNEL;

        //生成签名
        JSONObject json = new JSONObject();
        json.put("userNo",openId);
        json.put("boNo",boNo);
        json.put("totalPoint",point);
        json.put("ts",ts);
        //md5key
        String md5Key = "xuanzhen";
        String mySign = SignatureUtil.makeSignature(json,md5Key);

        RestTemplate restTemplate = new RestTemplate();
        JSONObject result = restTemplate.getForObject(APP_URL + "addPointStd?userNo=" + openId + "&boNo=" + boNo
                + "&count=" + point + "&ts=" + ts + "&sign=" + mySign+"&channel="+pppchannel, JSONObject.class);
        return result;

    }
}
